home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / BASICS.ZIP / CLASSLIB / CDOCUMNT.CPP < prev    next >
C/C++ Source or Header  |  1993-04-23  |  10KB  |  558 lines

  1. /*
  2.  * CDOCUMNT.CPP
  3.  * Sample Code Class Libraries
  4.  *
  5.  * Implementation of the CDocument class.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17.  
  18.  
  19. #include <windows.h>
  20. #include "classlib.h"
  21.  
  22.  
  23.  
  24. /*
  25.  * CDocument::CDocument
  26.  * CDocument::~CDocument
  27.  *
  28.  * Constructor Parameters:
  29.  *  hInst           HINSTANCE of the application.
  30.  *  uID             UINT identifier for the document
  31.  */
  32.  
  33. CDocument::CDocument(HINSTANCE hInst)
  34.     : CWindow(hInst)
  35.     {
  36.     m_pST=NULL;
  37.     m_pAdv=NULL;
  38.     return;
  39.     }
  40.  
  41.  
  42. CDocument::~CDocument(void)
  43.     {
  44.     if (NULL!=m_pST)
  45.         delete m_pST;
  46.  
  47.     return;
  48.     }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. /*
  56.  * CDocument::FInit
  57.  *
  58.  * Purpose:
  59.  *  Initializes an already created document window.  The client actually
  60.  *  creates the window then passes that here for further initialization.
  61.  *
  62.  * Parameters:
  63.  *  pDI             LPDOCUMENTINIT containing initialization parameters.
  64.  *
  65.  * Return Value:
  66.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  67.  */
  68.  
  69. BOOL CDocument::FInit(LPDOCUMENTINIT pDI)
  70.     {
  71.     if (NULL==pDI)
  72.         return FALSE;
  73.  
  74.     if (NULL==pDI->hWndDoc)
  75.         return FALSE;
  76.  
  77.     //Create our stringtable
  78.     m_pST=new CStringTable(m_hInst);
  79.  
  80.     if (!m_pST->FInit(pDI->idsMin, pDI->idsMax))
  81.         return FALSE;
  82.  
  83.     m_hWnd=pDI->hWndDoc;
  84.     m_pAdv=pDI->pAdv;
  85.     m_cf  =RegisterClipboardFormat(PSZ(IDS_CLIPBOARDFORMAT));
  86.  
  87.     m_fDirty=FALSE;
  88.     m_fNoDirty=FALSE;
  89.     m_fNoSize=FALSE;
  90.  
  91.     m_fFileKnown=FALSE;
  92.     m_szFile[0]=0;
  93.  
  94.     return TRUE;
  95.     }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. /*
  105.  * CDocument::FMessageHook
  106.  *
  107.  * Purpose:
  108.  *  Provides a derivation of the base CDocument class to hook all
  109.  *  messages to the window procedure for special processing, including
  110.  *  WM_COMMAND since that is much less common for a document than
  111.  *  for a frame (see CFrame::OnCommand).
  112.  *
  113.  * Parameters:
  114.  *  <WndProc Parameters>
  115.  *  pLRes           LRESULT FAR * in which to store the return value
  116.  *                  for the message.
  117.  *
  118.  * Return Value:
  119.  *  BOOL            TRUE to prevent further processing, FALSE otherwise.
  120.  */
  121.  
  122. BOOL CDocument::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  123.     , LPARAM lParam, LRESULT FAR *pLRes)
  124.     {
  125.     return FALSE;
  126.     }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. /*
  134.  * CDocument::FDirtySet
  135.  *
  136.  * Purpose:
  137.  *  Sets or clears the document 'dirty' flag returning the previous state
  138.  *  of that same flag.
  139.  *
  140.  * Parameters:
  141.  *  fDirty          BOOL indicating the new contents of the dirty flag.
  142.  *
  143.  * Return Value:
  144.  *  BOOL            Previous value of the dirty flag.
  145.  */
  146.  
  147. BOOL CDocument::FDirtySet(BOOL fDirty)
  148.     {
  149.     BOOL        fPrevious;
  150.  
  151.     /*
  152.      * If we are a hidden window, then there's nothing that could make
  153.      * us dirty since there cannot be any user interaction here.
  154.      */
  155.     if (!IsWindowVisible(m_hWnd))
  156.         return m_fDirty;
  157.  
  158.     //Ignore the initial WM_SIZE on creation.
  159.     if (m_fNoDirty)
  160.         return m_fDirty;
  161.  
  162.     fPrevious=m_fDirty;
  163.     m_fDirty=fDirty;
  164.  
  165.     return fPrevious;
  166.     }
  167.  
  168.  
  169.  
  170.  
  171.  
  172. /*
  173.  * CDocument::FDirtyGet
  174.  *
  175.  * Purpose:
  176.  *  Returns the current dirty status of the document.
  177.  *
  178.  * Parameters:
  179.  *  None
  180.  *
  181.  * Return Value:
  182.  *  BOOL            TRUE if the file is clean, FALSE otherwise.
  183.  */
  184.  
  185. BOOL CDocument::FDirtyGet()
  186.     {
  187.     return m_fDirty;
  188.     }
  189.  
  190.  
  191.  
  192.  
  193.  
  194. /*
  195.  * CDocument::Clear
  196.  *
  197.  * Purpose:
  198.  *  Sets all contents in the document back to defaults with no filename.
  199.  *
  200.  * Paramters:
  201.  *  None
  202.  *
  203.  * Return Value:
  204.  *  None
  205.  */
  206.  
  207. void CDocument::Clear()
  208.     {
  209.     FDirtySet(FALSE);
  210.     Rename(NULL);
  211.     return;
  212.     }
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. /*
  220.  * CDocument::ULoad
  221.  *
  222.  * Purpose:
  223.  *  Function that derived classes override.  Does nothing in base class.
  224.  *
  225.  * Parameters:
  226.  *  fChangeFile     BOOL indicating if we're to update the window title
  227.  *                  and the filename from using this file.
  228.  *  pszFile         LPSTR to the filename to load.  If NULL then this
  229.  *                  is a new, untitled file, so perform any initialization
  230.  *                  for such a case.
  231.  *
  232.  * Return Value:
  233.  *  UINT            An error value from DOCERR_.  Always DOCERR_NOFILE here.
  234.  */
  235.  
  236. UINT CDocument::ULoad(BOOL fChangeFile, LPSTR pszFile)
  237.     {
  238.     Rename(NULL);
  239.     return DOCERR_NOFILE;
  240.     }
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. /*
  249.  * CDocument::USave
  250.  *
  251.  * Purpose:
  252.  *  Function that derived classes override.  Does nothing in base class.
  253.  *
  254.  * Parameters:
  255.  *  uType           UINT type of file to save (from Save As dialog).
  256.  *  pszFile         LPSTR providing the name under which we should save.
  257.  *                  If NULL, then use the current name.  If non-NULL, then
  258.  *                  call Rename if save is successful and the name has changed.
  259.  *
  260.  * Return Value:
  261.  *  UINT            An error value from DOCERR_.  Always DOCERR_NOFILE here.
  262.  */
  263.  
  264. UINT CDocument::USave(UINT uType, LPSTR pszFile)
  265.     {
  266.     return DOCERR_NOFILE;
  267.     }
  268.  
  269.  
  270.  
  271.  
  272.  
  273. /*
  274.  * CDocument::ErrorMessage
  275.  *
  276.  * Purpose:
  277.  *  Displays an error message to the user appropriate for a given
  278.  *  error code.  If the code is DOCERR_NONE then this is a NOP.
  279.  *
  280.  * Parameters:
  281.  *  uErr            UINT identifying the error code.
  282.  *
  283.  * Return Value:
  284.  *  None
  285.  */
  286.  
  287. void CDocument::ErrorMessage(UINT uErr)
  288.     {
  289.     LPSTR       psz;
  290.  
  291.     switch (uErr)
  292.         {
  293.         case DOCERR_NONE:
  294.             psz=NULL;
  295.             break;
  296.  
  297.         case DOCERR_NOFILE:
  298.             psz=PSZ(IDS_FILEDOESNOTEXIST);
  299.             break;
  300.  
  301.         case DOCERR_COULDNOTOPEN:
  302.             psz=PSZ(IDS_FILEOPENERROR);
  303.             break;
  304.  
  305.         case DOCERR_READFAILURE:
  306.             psz=PSZ(IDS_FILELOADERROR);
  307.             break;
  308.  
  309.         case DOCERR_UNSUPPORTEDVERSION:
  310.             psz=PSZ(IDS_VERSIONMISMATCH);
  311.             break;
  312.  
  313.         case DOCERR_WRITEFAILURE:
  314.             psz=PSZ(IDS_FILESAVEERROR);
  315.             break;
  316.  
  317.         case DOCERR_CANCELLED:
  318.             //No message on this.
  319.             return;
  320.  
  321.         default:
  322.             psz=PSZ(IDS_UNKNOWNERROR);
  323.             break;
  324.         }
  325.  
  326.     if (NULL!=psz)
  327.         MessageBox(m_hWnd, psz, PSZ(IDS_DOCUMENTCAPTION), MB_OK);
  328.  
  329.     return;
  330.     }
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341. /*
  342.  * CDocument::FClip
  343.  *
  344.  * Purpose:
  345.  *  Places document data on the clipboard, optionally implementing
  346.  *  Cut by deleting the data in the current window after rendering.
  347.  *
  348.  * Parameters:
  349.  *  hWndFrame       HWND of the main window.
  350.  *  fCut            BOOL indicating cut (TRUE) or copy (FALSE).
  351.  *
  352.  * Return Value:
  353.  *  BOOL            TRUE if successful, FALSE otherwise.
  354.  */
  355.  
  356. BOOL CDocument::FClip(HWND hWndFrame, BOOL fCut)
  357.     {
  358.     return FALSE;
  359.     }
  360.  
  361.  
  362.  
  363.  
  364. /*
  365.  * CDocument::RenderFormat
  366.  *
  367.  * Purpose:
  368.  *  Returns a global memory handle containing a specific clipboard
  369.  *  format.
  370.  *
  371.  * Parameters:
  372.  *  cf              UINT format to render
  373.  *
  374.  * Return Value:
  375.  *  HGLOBAL         Memory handle of the rendering.
  376.  */
  377.  
  378. HGLOBAL CDocument::RenderFormat(UINT cf)
  379.     {
  380.     return NULL;
  381.     }
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388. /*
  389.  * CDocument::FQueryPaste
  390.  *
  391.  * Purpose:
  392.  *  Determines if we can paste data from the clipboard.
  393.  *
  394.  * Parameters:
  395.  *  None
  396.  *
  397.  * Return Value:
  398.  *  BOOL            TRUE if data is available, FALSE otherwise.
  399.  */
  400.  
  401. BOOL CDocument::FQueryPaste(void)
  402.     {
  403.     return IsClipboardFormatAvailable(m_cf);
  404.     }
  405.  
  406.  
  407.  
  408.  
  409.  
  410. /*
  411.  * CDocument::FPaste
  412.  *
  413.  * Purpose:
  414.  *  Retrieves the private data format from the clipboard and sets it
  415.  *  as the current data.
  416.  *
  417.  *  Note that if this function is called, then the clipboard format
  418.  *  is available because the Paste menu item is only enabled if the
  419.  *  format is present.
  420.  *
  421.  * Parameters:
  422.  *  hWndFrame       HWND of the main window
  423.  *
  424.  * Return Value:
  425.  *  BOOL            TRUE if successful, FALSE otherwise.
  426.  */
  427.  
  428. BOOL CDocument::FPaste(HWND hWndFrame)